import { articleModel } from "../Model/artModel.js"

export const addArtDao = async(article)=>{
    return await articleModel.create(article);
}
export const delArtDao = async(id)=>{
    return await articleModel.findOneAndDelete(id);
}

export const updateArtDao = async(id,obj)=>{
    return await articleModel.findByIdAndUpdate(id,obj);
}


/**
 * 
 * @param {Number} page 
 * @param {Number} size 
 * @param {
 *      typename 类型名称
 *      title 文章标题 模糊查找
 * } queryobj 
 * @returns 
 */
export const getArtByPageDao = async(page,size,queryobj = {})=>{
    if(queryobj.title){
        queryobj.title = new RegExp(queryobj.title,"i");
    }
    const list = await articleModel.find(queryobj).skip((page-1)*size).limit(size);
    const total = await articleModel.find(queryobj).countDocuments();
    return {
        list,
        total
    }
}
export const getArtByIdDao = async (id)=>{
    //通过id搜索对象，给pv加1
    await articleModel.findByIdAndUpdate(id,{$inc:{pv:1}})
    return await articleModel.findById(id)
}